﻿/*-------------------------------------
//
//CopyLeft 2007 开发二组 
//
//文件名：ReceiptManage.cs
//
//文件功能描述：收款单管理.
// 
//创建标识：2007.11.21 曾健铭
//
//修改标识: 2007.11.28 曾健铭
//修改描述: 修正Bug,优化代码逻辑,按规范重新编码..
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_ReceiptManage : System.Web.UI.UserControl
{
    private int PAGEINDEX = 1;
    private int PAGESIZE = 10;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //设置默认加载页面的搜索条件.
            string strStartTime = DateTime.Now.AddDays(-30).ToShortDateString();
            string strEndTime = DateTime.Now.ToString();
            DataSet ds = (new localhost.DocumentService()).GetReceiptListForPaging(strStartTime, strEndTime, PAGEINDEX, PAGESIZE);
            BindDataToGridView(ds);

            hdfReceiptQuantity.Value = (new localhost.DocumentService()).GetReceiptQuantity(strStartTime, strEndTime).ToString();
            int iPageIndex = GetPageIndex();
            SetControlsProperty(iPageIndex);
            //CallWebService(strStartTime,strEndTime,PAGEINDEX);
        }
    }

    /// <summary>
    /// 按条件搜索收款单r
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        //获取用户输入
        string strReceiptID = txtReceiptID.Text.Trim();
        string strStartTime = string.Empty;
        string strEndTime = string.Empty;
        
        if (strReceiptID == string.Empty)//如果没有输入收款单ID,则按默认的时间段进行搜索.
        {
            ValidateTime(out strStartTime, out strEndTime);
            //如果用户输入的两个时间是一样的,表明他想搜索该天的收款单,按此想法设置搜索条件.
            if (strStartTime == strEndTime)
            {
                strEndTime = DateTime.Now.ToString();
                try
                {
                    strEndTime = Convert.ToDateTime(strStartTime).AddDays(1).ToString();
                }
                catch
                {
                    strStartTime = string.Empty;
                }
            }
            SetSearchCondition(strStartTime, strEndTime);
            Session["ReceiptPageIndex"] = 1;
            CallWebService(strStartTime, strEndTime,PAGEINDEX);//异步调用
        }
        else
        {
            CallWebService(strReceiptID);//用户输入了收款单ID,按该ID来搜索.
        }
    }
    /// <summary>
    /// 删除收款单
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        bool blnDeleted = false;//判断删除是否成功
        string strReceiptID = ((ImageButton)sender).CommandArgument;//获取收款单号
        try
        {
            blnDeleted = (new localhost.DocumentService()).DeleteReceipt(strReceiptID);//执行删除操作
        }
        catch
        {
        }
        if (blnDeleted)//如果删除成功,则刷新页面
        {
            string strStartTime = DateTime.Now.AddDays(-1).ToString();
            string strEndTime = DateTime.Now.ToString();
            GetSearchCondition(out strStartTime,out strEndTime);

            int iPageIndex = GetPageIndex();

            CallWebService(strStartTime, strEndTime,iPageIndex);
            //弹出提示信息
            //lblInfo.Text = "删除成功";
        }
        else
        {
            //删除失败,弹出提示信息
            Page.RegisterStartupScript("warnings", "<script>alert('删除失败！');</script>");
            //lblInfo.Text = "删除失败";
        }
    }

    /// <summary>
    /// 异步调用,按时间段查询
    /// </summary>
    /// <param name="strStartTime">搜索的起始时间</param>
    /// <param name="strEndTime">搜索的结束时间</param>
    private void CallWebService(string strStartTime,string strEndTime,int iPageIndex)
    {
        localhost.DocumentService service = new localhost.DocumentService();
        service.GetReceiptListForPagingCompleted += new localhost.GetReceiptListForPagingCompletedEventHandler(service_GetReceiptListForPagingCompleted);//声明回调函数
        service.GetReceiptListForPagingAsync(strStartTime, strEndTime, iPageIndex, PAGESIZE);//调用方法,按搜索条件返回收款单列表
    }
    /// <summary>
    /// 回调函数,当收款单列表返回后,将数据绑定到GridView中
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void service_GetReceiptListForPagingCompleted(object sender, localhost.GetReceiptListForPagingCompletedEventArgs e)
    {
        BindDataToGridView(e.Result);//绑定数据

        string strStartTime = string.Empty;
        string strEndTime = string.Empty;

        GetSearchCondition(out strStartTime,out strEndTime);

        hdfReceiptQuantity.Value = (new localhost.DocumentService()).GetReceiptQuantity(strStartTime, strEndTime).ToString();

        int iPageIndex = GetPageIndex();
        
        SetControlsProperty(iPageIndex);
    }
    /// <summary>
    /// 异步调用,按照收款单ID查询
    /// </summary>
    /// <param name="strReceiptID"></param>
    private void CallWebService(string strReceiptID)
    {
        localhost.DocumentService service = new localhost.DocumentService();
        service.GetReceiptListByReceiptIDCompleted += new localhost.GetReceiptListByReceiptIDCompletedEventHandler(service_GetReceiptListByIDCompleted);//声明回调函数
        service.GetReceiptListByReceiptIDAsync(strReceiptID);
    }
    /// <summary>
    /// 回调函数,当收款单列表返回后,将数据绑定到GridView中
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void service_GetReceiptListByIDCompleted(object sender, localhost.GetReceiptListByReceiptIDCompletedEventArgs e)
    {

        BindDataToGridView(e.Result);//绑定数据
        if (e.Result == null)
        {
            hdfReceiptQuantity.Value = "0";
        }
        else
        {
            hdfReceiptQuantity.Value = e.Result.Tables[0].Rows.Count.ToString();
        }
        SetControlsProperty(PAGEINDEX);
    }

    /// <summary>
    /// 设置控件的可见和显示文本的属性.
    /// </summary>
    /// <param name="iPageIndex"></param>
    /// <param name="strStartTime"></param>
    /// <param name="strEndTime"></param>
    /// <param name="blnDefault"></param>
    /// <param name="iStatus"></param>
    private void SetControlsProperty(int iPageIndex)
    {
        int iReceiptQuantit = 0;
        try
        {
            iReceiptQuantit = Convert.ToInt32(hdfReceiptQuantity.Value);
        }
        catch
        {
            iReceiptQuantit = 0;
        }
        if (iReceiptQuantit > 0)
        {
            HaveData(iReceiptQuantit, iPageIndex, PAGESIZE);
            int iCurrentPageIndex = GetCurrentPageIndex();
            int iTotalPages = GetTotalPages();

            if (iCurrentPageIndex == 1)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
            }
            if (iCurrentPageIndex == iTotalPages)
            {
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }

        }
        else
        {
            NoData();
        }

    }
    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="dsBind"></param>
    private void BindDataToGridView(DataSet dsBind)
    {
        gdvReceipt.DataSource = dsBind;
        gdvReceipt.DataBind();
    }

    /// <summary>
    /// 没有数据时,设置Label是否可见
    /// </summary>
    private void NoData()
    {
        btnFirst.Visible = false;
        btnPrev.Visible = false;
        btnNext.Visible = false;
        btnLast.Visible = false;

        lblCurrentIndex.Visible = false;
        lblPageCount.Visible = false;
        lblRecordCount.Visible = false;

        lblNoRecord.Visible = true;
    }

    /// <summary>
    /// 有数据,设置控件的属性
    /// </summary>
    /// <param name="iInvoiceQuantity">发货单数量</param>
    /// <param name="iPageIndex">第几页</param>
    /// <param name="iPageSize">页面大小</param>
    private void HaveData(int iReceiptQuantit, int iPageIndex, int iPageSize)
    {
        lblRecordCount.Text = "共 " + iReceiptQuantit.ToString() + " 条";
        lblRecordCount.Visible = true;
        lblPageCount.Visible = true;
        lblNoRecord.Visible = false;

        //
        //计算总页数
        //
        int iPages = iReceiptQuantit / iPageSize;
        if (iReceiptQuantit % iPageSize != 0)
        {
            iPages += 1;
        }//if(iOrders % iPageSize != 0)
        lblPageCount.Text = "共 " + iPages.ToString() + " 页";
        lblPageCount.Visible = true;

        //
        //有多页
        //
        if (iPages > 1)
        {
            btnFirst.Visible = true;
            btnPrev.Visible = true;
            btnNext.Visible = true;
            btnLast.Visible = true;

            btnNext.Enabled = true;
            btnPrev.Enabled = true;
            btnLast.Enabled = true;
            btnFirst.Enabled = true;
        }
        else
        {
            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;
        }

        //
        //当前页
        //
        lblCurrentIndex.Text = "第 " + iPageIndex + " 页";
        lblCurrentIndex.Visible = true;
    }
    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        int iCurrentPageIndex = GetCurrentPageIndex();
        int iTotalPages = GetTotalPages();

        string arg = ((LinkButton)sender).CommandArgument.ToString();//获取按钮参数
        switch (arg)
        {
            case "prev"://上一页
                if (iCurrentPageIndex > 1)
                {
                    iCurrentPageIndex -= 1;
                }
                break;
            case "next"://下一页
                if (iCurrentPageIndex < iTotalPages)
                {
                    iCurrentPageIndex += 1;
                }
                break;
            case "last"://最后一页
                iCurrentPageIndex = iTotalPages;
                break;
            default://首页
                iCurrentPageIndex = 1;
                break;
        }

        //初始化变量
        string strStartTime = string.Empty;
        string strEndTime = string.Empty;
        Session["ReceiptPageIndex"] = iCurrentPageIndex.ToString();
        GetSearchCondition(out strStartTime, out strEndTime);
        CallWebService(strStartTime, strEndTime,iCurrentPageIndex);
    }

    /// <summary>
    /// 将搜索条件保存到隐藏控件中
    /// </summary>
    /// <param name="strStartTime">搜索起始时间</param>
    /// <param name="strEndTime">搜索结束时间</param>
    private void SetSearchCondition(string strStartTime, string strEndTime)
    {
        hdfStartTime.Value = strStartTime;
        hdfEndTime.Value = strEndTime;
    }

    /// <summary>
    /// 获取隐藏控件中保存的搜索条件
    /// </summary>
    /// <param name="strStartTime">搜索起始时间</param>
    /// <param name="strEndTime">搜索结束时间</param>
    private void GetSearchCondition(out string strStartTime, out string strEndTime)
    {
        strStartTime = hdfStartTime.Value;
        strEndTime = hdfEndTime.Value;
    }

    /// <summary>
    /// 获取当前页
    /// </summary>
    /// <returns>当前页</returns>
    private int GetCurrentPageIndex()
    {
        string strPageIndex = lblCurrentIndex.Text.Trim();
        strPageIndex = strPageIndex.Substring(2);
        strPageIndex = strPageIndex.Substring(0, strPageIndex.Length - strPageIndex.LastIndexOf(" "));
        int iCurrentPage = Convert.ToInt32(strPageIndex);
        return iCurrentPage;
    }

    /// <summary>
    /// 获取总的页数
    /// </summary>
    /// <returns>总页数</returns>
    private int GetTotalPages()
    {
        string strPageCount = lblPageCount.Text.Trim();
        strPageCount = strPageCount.Substring(2);
        strPageCount = strPageCount.Substring(0, strPageCount.Length - strPageCount.LastIndexOf(" "));
        int iTotalPages = Convert.ToInt32(strPageCount);
        return iTotalPages;
    }

    /// <summary>
    /// 验证时间格式是否正确,如果不正确,则重新设置时间段
    /// </summary>
    /// <param name="strStartTime"></param>
    /// <param name="strEndTime"></param>
    /// <returns></returns>
    private void ValidateTime(out string strStartTime, out string strEndTime)
    {
        strStartTime = txtStartTime.Text.Trim();
        strEndTime = txtEndTime.Text.Trim();

        if (strStartTime == string.Empty && strEndTime == string.Empty)
        {
            strStartTime = DateTime.Now.AddDays(-30).ToShortDateString();
            strEndTime = DateTime.Now.ToString();
        }
        else
        {
            if (strEndTime == string.Empty)
            {
                strEndTime = DateTime.Now.AddDays(1).ToShortDateString();
            }
            else
            {
                try
                {
                    strEndTime = Convert.ToDateTime(strEndTime).ToShortDateString();
                }
                catch
                {
                    strEndTime = DateTime.Now.AddDays(1).ToShortDateString();
                }
            }
            if (strStartTime == string.Empty)
            {
                strStartTime = Convert.ToDateTime(strEndTime).AddDays(-2).ToShortDateString();
            }
            else
            {
                try
                {
                    //强制转换日期格式,以判断格式是否正确
                    strStartTime = Convert.ToDateTime(strStartTime).ToShortDateString();

                }
                catch
                {
                    //日期格式不正确,设置成默认的搜索时间段
                    strStartTime = DateTime.Now.AddDays(-1).ToShortDateString();
                }
            }
        }
    }

    /// <summary>
    /// 判断当前是第几页
    /// </summary>
    /// <returns>当前页的序号</returns>
    private int GetPageIndex()
    {
        //初始化变量
        int iPageIndex = 1;
        string strPageIndex = string.Empty;
        try
        {
            strPageIndex = Session["ReceiptPageIndex"].ToString();//从Session中读取
        }
        catch
        {
            Session["ReceiptPageIndex"] = 1;//发生异常,说明为第一页
            strPageIndex = "1";
        }
        if (strPageIndex != string.Empty)
        {
            iPageIndex = Convert.ToInt32(strPageIndex);//获取页的序号
        }
        return iPageIndex;
    }
}
